package com.macro.mall.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsMuseumTeamDetail;
import com.macro.mall.dto.BmsMuseumTeamParam;
import com.macro.mall.mapper.BmsMuseumApplicantMapper;
import com.macro.mall.mapper.BmsMuseumTeamMapper;
import com.macro.mall.model.BmsMuseumApplicant;
import com.macro.mall.model.BmsMuseumTeam;
import com.macro.mall.model.BmsMuseumTeamExample;
import com.macro.mall.portal.service.BmsMuseumTeamService;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class BmsMuseumTeamServiceImpl implements BmsMuseumTeamService {

    @Autowired
    private BmsMuseumTeamMapper bmsMuseumTeamMapper;

    @Autowired
    private BmsMuseumApplicantMapper bmsMuseumApplicantMapper;

    @Value("${data_template.team_appointment}")
    private String teamAppointment;

    @Override
    public List<BmsMuseumTeam> getList(BmsMuseumTeamParam bmsMuseumTeamParam) {
        if (Objects.isNull(bmsMuseumTeamParam.getMuseumGroupId())) {
            return Lists.newArrayList();
        }
        Integer pageNum = bmsMuseumTeamParam.getPageNum();
        Integer pageSize = bmsMuseumTeamParam.getPageSize();
        if (Objects.nonNull(pageNum) && Objects.nonNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }

        BmsMuseumTeamExample example = new BmsMuseumTeamExample();
        BmsMuseumTeamExample.Criteria criteria = example.createCriteria();
        criteria.andMuseumGroupIdEqualTo(bmsMuseumTeamParam.getMuseumGroupId());
        List<BmsMuseumTeam> list = bmsMuseumTeamMapper.selectByExample(example);
        return list;
    }

    @Override
    public BmsMuseumTeamDetail getItem(Long id) {
        BmsMuseumTeam bmsMuseumTeam = bmsMuseumTeamMapper.selectByPrimaryKey(id);
        BmsMuseumTeamDetail detail = new BmsMuseumTeamDetail();
        BeanUtils.copyProperties(bmsMuseumTeam, detail);
        return detail;
    }

    /**
     * 查询团队模板下载url
     */
    @Override
    public String getTemplateUrl(){
        return teamAppointment;
    }

    /**
     * 根据团队模板文件保存团队预约数据
     * @author WangShanxi
     * @date 2024/8/19
     * @param bmsMuseumTeam
     * @param file
     * @return
     */
    @Override
    @Transactional(rollbackFor = {})
    public int uploadMuseumTeamByFile(BmsMuseumTeam bmsMuseumTeam, MultipartFile file) throws Exception{
        if (file.isEmpty()) {
            throw new Exception("文件不能为空");
        }
        bmsMuseumTeamMapper.insertSelective(bmsMuseumTeam);
        Long teamId = bmsMuseumTeam.getId();
        List<BmsMuseumApplicant> list=new ArrayList<>();
        BmsMuseumApplicant bma;
        try (InputStream inputStream = file.getInputStream();
            Workbook workbook = new XSSFWorkbook(inputStream)) {
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                // 跳过标题行，如果有的话
                if (row.getRowNum() == 0) {
                    continue;
                }
                //姓名
                Cell cell1 = row.getCell(0);
                //证件类型 1身份证
                //Cell cell2 = row.getCell(1);
                //证件号
                Cell cell3 = row.getCell(2);
                //手机号
                Cell cell4 = row.getCell(3);
                if (cell1 != null  && cell3 != null && cell4 != null) {
                    String value1="";
                    try {
                        value1 = cell1.getStringCellValue();
                    }catch(Exception e) {
                        DecimalFormat df = new DecimalFormat("#");
                        value1 = df.format(cell1.getNumericCellValue());
                    }
                    String value3 ="";
                    try {
                        value3 = cell3.getStringCellValue();
                    }catch(Exception e) {
                        DecimalFormat df = new DecimalFormat("#");
                        value3 = df.format(cell3.getNumericCellValue());
                    }
                    String phone="";
                    try {
                        phone = cell4.getStringCellValue();
                    }catch(Exception e) {
                        DecimalFormat df = new DecimalFormat("#");
                        phone = df.format(cell4.getNumericCellValue());
                    }
                    bma = new BmsMuseumApplicant();
                    bma.setMuseumGroupId(bmsMuseumTeam.getMuseumGroupId());
                    bma.setApplicationType("02");
                    bma.setMuseumTeamId(teamId);
                    bma.setUserName(value1);
                    //默认写死1身份证
                    bma.setUserIdType(1L);
                    bma.setUserIdCard(value3);
                    bma.setUserPhone(phone);
                    bma.setApplicationTime(bmsMuseumTeam.getApplicationTime());
                    list.add(bma);
                }
            }
        }
        int result =bmsMuseumApplicantMapper.batchInsert(list);
        return result;
    }
}
